package jmine.tec.persist.jms;

import javax.jms.Message;

/**
 * MDB Persistente capaz de identificar e registrar mensagens de testes.
 * 
 * @author queiroz
 */
public class MonitoredPersistentMessageMDB extends PersistentMessageMDB {

    /**
     * {@inheritDoc}
     */
    @Override
    public void onMessage(Message msg) {
        final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        final ClassLoader beanFactoryClassLoader = getClass().getClassLoader();
        if (currentClassLoader != beanFactoryClassLoader) {
            Thread.currentThread().setContextClassLoader(beanFactoryClassLoader);
        }
        try {
            if (TestMessageSender.isTestMessage(msg)) {
                new TestMessageSender(this.getTransactionalController()).monitor(msg, this.getMessageDrivenContext().toString());
            } else {
                super.onMessage(msg);
            }
        } finally {
            if (currentClassLoader != beanFactoryClassLoader) {
                Thread.currentThread().setContextClassLoader(currentClassLoader);
            }
        }

    }

}
